package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.ReactiveStringCommands;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImp implements CategoryService {
        @Autowired
    CategoryMapper categoryMapper;
        @Autowired
    DishMapper dishMapper;
        @Autowired
    SetmealMapper setmealMapper;
        /*
        * 添加数据
        * */
    @Override
    public void sava(CategoryDTO categoryDTO) {
        Category category = new Category();
        //数据拷贝
        BeanUtils.copyProperties(categoryDTO,category);
        //添加其他数据
        //状态
        category.setStatus(StatusConstant.DISABLE);
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.sava(category);
    }

    @Override
    public PageResult select(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
       Page<Category> p = categoryMapper.select(categoryPageQueryDTO);
        long total = p.getTotal();
        List<Category> result = p.getResult();
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(result);
        return pageResult;
    }

    @Override
    public void Delte(long id) {
        /*
        * 根据id删除
        * */
        // 要看一下是否关联套餐表
           Integer cont = setmealMapper.select(id);
           if (cont>0){
               throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
           }
        // 要看一下是否关联菜单表
        Integer conts = dishMapper.select(id);
        if (conts>0){
            throw  new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        categoryMapper.Delete(id);
    }
/*
* 根据id修改
* */
    @Override
    public void update(CategoryDTO categoryDTO) {
        //要转换对象
        Category category = new Category();
        //对象属性kaob
        BeanUtils.copyProperties(categoryDTO,category);
        //设置其他属性
//        category.setUpdateUser(BaseContext.getCurrentId());
//        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }
/*
* 启用 禁用
* */
    @Override
    public void status(Integer status, long id) {
        Category category = new Category();
        category.setId(id);
        category.setStatus(status);
//        category.setUpdateUser(BaseContext.getCurrentId());
//        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }
/*
* 根据类型查询数据
* */
    @Override
    public List<Category> list(Integer type) {
        List<Category> list = categoryMapper.list(type);
        return list;
    }

}
